//
//  DZTabBarController.m
//  花田小憩
//
//  Created by 董筱泽 on 2017/9/14.
//  Copyright © 2017年 董筱泽. All rights reserved.
//

#import "DZTabBarController.h"
#import "DZNavigationController.h"

@interface DZTabBarController ()

@end

@implementation DZTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tabBar.tintColor = [UIColor blackColor];
    self.tabBar.translucent = NO;
    self.tabBar.backgroundColor = [UIColor whiteColor];
    // 创建四个子控制器并设置它的相应内容
    UIViewController *vc1 = [self loadChildViewControllerWithClassName:@"DZHomeController" andTitle:@"专题" andImageName:@"tb_0"];
    
    
    UIViewController *vc2 = [self loadChildViewControllerWithClassName:@"DZMineController" andTitle:@"商城" andImageName:@"tb_1"];
    
    UIViewController *vc3 = [self loadChildViewControllerWithClassName:@"DZShopController" andTitle:@"新鲜预定" andImageName:@"tb_2"];
    // 给标签控制器添加子控制器
    self.tabBarItem.tag = self.childViewControllers.count ;
    self.viewControllers = @[vc1, vc2, vc3];
    
    // 让tabBar没有半透明效果,还会影响它里面view的高度"默认view的最底部是贴着屏幕的最底部,如果氢此属性设置为NO,view的底部只到tabBar的顶部"
    self.tabBar.translucent = NO;
    
    
}
- (UIViewController *)loadChildViewControllerWithClassName:(NSString *)className andTitle:(NSString *)title andImageName:(NSString *)imageName {
    
    
    // 把类名的字符串转成类的类型
    Class class =  NSClassFromString(className);
    // 通过转换出来的类的类型来创建控制器
    UIViewController *vc = [[class alloc] init];
    
    // 返回导航控制器"导航控制器中又添加了一个根控制器"
    return [self setupWithViewController:vc andTitle:title andImageName:imageName];
}

- (UIViewController *)setupWithViewController:(UIViewController *)vc andTitle:(NSString *)title andImageName:(NSString *)imageName {
    // 设置标签栏上对应控制器的标题
    vc.tabBarItem.title = title;
    
    
    // 创建导航控制器
    DZNavigationController *nav = [[DZNavigationController alloc] initWithRootViewController:vc];
    nav.navigationBar.translucent = NO;
    nav.tabBarItem.title = title;
    // 设置默认状态的图片
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    nav.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]];
    vc.tabBarItem.tag = self.childViewControllers.count ;
    return nav;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
